Practice using ggplot and ggmap

臺灣地圖

library(ggmap)
## Loading required package: ggplot2
library(mapproj)
## Loading required package: maps
map_1 <- get_map(location = c(lon = 120.233937, lat = 22.993013), zoom = 5)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=22.993013,120.233937&zoom=5&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
ggmap(map_1)

臺灣道路地圖_臺北101

map_2 <- get_map(location = c(lon = 121.564472, lat = 25.033964),
  zoom = 18, language = "zh-TW", maptype = "roadmap")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=25.033964,121.564472&zoom=18&size=640x640&scale=2&maptype=roadmap&language=zh-TW&sensor=false
ggmap(map_2)

臺灣衛星地圖_臺北101

map_3 <- get_map(location = c(lon = 121.564472, lat = 25.033964),
  zoom = 18, language = "zh-TW", maptype = "satellite")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=25.033964,121.564472&zoom=18&size=640x640&scale=2&maptype=satellite&language=zh-TW&sensor=false
ggmap(map_3)

臺灣獨立書店

獨立書店分布圖

library(readr)

bs <- read_csv("book.csv")
## Parsed with column specification:
## cols(
##   .default = col_character(),
##   郵遞區號 = col_integer(),
##   經度 = col_double(),
##   緯度 = col_double(),
##   點閱數 = col_integer()
## )
## See spec(...) for full column specifications.
lon.deg <- sapply((strsplit(as.character(bs$"經度"), ",")), as.numeric)
lat.deg <- sapply((strsplit(as.character(bs$"緯度"), ",")), as.numeric)
bs$lat <- lat.deg
bs$lon <- lon.deg

library(ggmap)
map_4 <- get_map(location = c(lon = 120.5, lat = 23.625), zoom = 8)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=23.625,120.5&zoom=8&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
ggmap(map_4) + geom_point(aes(x = lon, y = lat), data = bs)
## Warning: Removed 35 rows containing missing values (geom_point).

獨立書店分布圖,點大小呈現點閱數多寡

點越大,即點閱的次數越多

library(readr)

bs <- read_csv("book.csv")
## Parsed with column specification:
## cols(
##   .default = col_character(),
##   郵遞區號 = col_integer(),
##   經度 = col_double(),
##   緯度 = col_double(),
##   點閱數 = col_integer()
## )
## See spec(...) for full column specifications.
lon.deg <- sapply((strsplit(as.character(bs$"經度"), ",")), as.numeric)
lat.deg <- sapply((strsplit(as.character(bs$"緯度"), ",")), as.numeric)
bs$lat <- lat.deg
bs$lon <- lon.deg

library(ggmap)
map_4 <- get_map(location = c(lon = 120.5, lat = 23.625), zoom = 8)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=23.625,120.5&zoom=8&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
ggmap(map_4) + geom_point(aes(x = lon, y = lat, size = "點閱數"), data = bs)
## Warning: Using size for a discrete variable is not advised.
## Warning: Removed 35 rows containing missing values (geom_point).

獨立書店分布圖與點閱數之關聯

淡化地圖,能更清楚呈現資料

ggmap(map_4, darken = c(0.5, "white")) +
  geom_point(aes(x = lon, y = lat, size = "點閱數"), data = bs)
## Warning: Using size for a discrete variable is not advised.
## Warning: Removed 35 rows containing missing values (geom_point).

## 信義區(位置lon = 121.576957, lat = 25.028702) ### 由下圖可見,信義區附近沒有獨立書店

library(readr)

bs <- read_csv("book.csv")
## Parsed with column specification:
## cols(
##   .default = col_character(),
##   郵遞區號 = col_integer(),
##   經度 = col_double(),
##   緯度 = col_double(),
##   點閱數 = col_integer()
## )
## See spec(...) for full column specifications.
lon.deg <- sapply((strsplit(as.character(bs$"經度"), ",")), as.numeric)
lat.deg <- sapply((strsplit(as.character(bs$"緯度"), ",")), as.numeric)
bs$lat <- lat.deg
bs$lon <- lon.deg

library(ggmap)
map_5 <- get_map(location = c(lon = 121.576957, lat = 25.028702), zoom = 15)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=25.028702,121.576957&zoom=15&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
ggmap(map_5) + geom_point(aes(x = lon, y = lat), data = bs)
## Warning: Removed 158 rows containing missing values (geom_point).

中正區(lon = 121.519872, lat =25.042141)

資料顯示中正區有許多獨立書店分布,且與信義區比較,中正區也明顯高出很多

library(readr)

bs <- read_csv("book.csv")
## Parsed with column specification:
## cols(
##   .default = col_character(),
##   郵遞區號 = col_integer(),
##   經度 = col_double(),
##   緯度 = col_double(),
##   點閱數 = col_integer()
## )
## See spec(...) for full column specifications.
lon.deg <- sapply((strsplit(as.character(bs$"經度"), ",")), as.numeric)
lat.deg <- sapply((strsplit(as.character(bs$"緯度"), ",")), as.numeric)
bs$lat <- lat.deg
bs$lon <- lon.deg

library(ggmap)
map_6 <- get_map(location = c(lon = 121.519872, lat =25.042141), zoom = 13)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=25.042141,121.519872&zoom=13&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
ggmap(map_6) + geom_point(aes(x = lon, y = lat), data = bs)
## Warning: Removed 109 rows containing missing values (geom_point).

大安區(lon = 121.542709, lat = 25.026158)

大安區與中正區鄰近,有一些獨立書店的分佈

library(readr)

bs <- read_csv("book.csv")
## Parsed with column specification:
## cols(
##   .default = col_character(),
##   郵遞區號 = col_integer(),
##   經度 = col_double(),
##   緯度 = col_double(),
##   點閱數 = col_integer()
## )
## See spec(...) for full column specifications.
lon.deg <- sapply((strsplit(as.character(bs$"經度"), ",")), as.numeric)
lat.deg <- sapply((strsplit(as.character(bs$"緯度"), ",")), as.numeric)
bs$lat <- lat.deg
bs$lon <- lon.deg

library(ggmap)
map_5 <- get_map(location = c(lon = 121.542709, lat = 25.026158), zoom = 15)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=25.026158,121.542709&zoom=15&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
ggmap(map_5) + geom_point(aes(x = lon, y = lat), data = bs)
## Warning: Removed 129 rows containing missing values (geom_point).

臺中市(lon = 120.673648, lat =24.147736)

從資料中,臺中市是獨立書店次多的地區,透過下圖呈現分佈

library(readr)

bs <- read_csv("book.csv")
## Parsed with column specification:
## cols(
##   .default = col_character(),
##   郵遞區號 = col_integer(),
##   經度 = col_double(),
##   緯度 = col_double(),
##   點閱數 = col_integer()
## )
## See spec(...) for full column specifications.
lon.deg <- sapply((strsplit(as.character(bs$"經度"), ",")), as.numeric)
lat.deg <- sapply((strsplit(as.character(bs$"緯度"), ",")), as.numeric)
bs$lat <- lat.deg
bs$lon <- lon.deg

library(ggmap)
map_6 <- get_map(location = c(lon = 120.673648, lat =24.147736), zoom = 14)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=24.147736,120.673648&zoom=14&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
ggmap(map_6) + geom_point(aes(x = lon, y = lat), data = bs)
## Warning: Removed 148 rows containing missing values (geom_point).

結論

臺灣獨立書店主要分佈在北部地區,尤其以臺北市中正區為多


資料來源:政府資料開放平台https://data.gov.tw/dataset/6224